ostree/config: Add an "unset" operation
authorMatthew Leeds <matthew.leeds@endlessm.com>
Fri, 28 Sep 2018 22:36:49 +0000 (15:36 -0700)
committerAtomic Bot <atomic-devel@projectatomic.io>
Fri, 1 Mar 2019 16:29:44 +0000 (16:29 +0000)
Currently there's a way to set a key to the empty string but there's no
way to unset it completely (remove the key from the group). This might
be helpful for instance if you want to temporarily set
"core.lock-timeout-secs" to a specific value for the duration of one
operation and then return it to the default after that operation
completes.

This commit implements an "unset" operation for the config command, adds
a unit test, and updates the man page.

Closes: #1743
Approved by: cgwalters

man/ostree-config.xml
src/ostree/ot-builtin-config.c
tests/test-config.sh

index dc180b83bafab7577d68ec6b836aaf6f89f2272a..f2b41ec1c660f203500e900ee37ddf6c958d3c83 100644 (file)
@@ -62,14 +62,37 @@ Boston, MA 02111-1307, USA.
             <cmdsynopsis>
                 <command>ostree config set</command> <arg choice="req"> --group=GROUPNAME</arg> <arg choice="req"> KEYNAME</arg> <arg choice="req">VALUE</arg>
             </cmdsynopsis>
+            <cmdsynopsis>
+                <command>ostree config unset</command> <arg choice="req">SECTIONNAME.KEYNAME</arg>
+            </cmdsynopsis>
+            <cmdsynopsis>
+                <command>ostree config unset</command> <arg choice="req"> --group=GROUPNAME</arg> <arg choice="req"> KEYNAME</arg>
+            </cmdsynopsis>
     </refsynopsisdiv>
 
     <refsect1>
         <title>Description</title>
 
-        <para>
-            Displays or changes a configuration setting.
-        </para>
+        <itemizedlist>
+            <listitem><para>
+                <command>ostree config get</command> displays the value of
+                <arg choice="plain">KEYNAME</arg> in the group <arg choice="plain">GROUPNAME</arg>
+                (or <arg choice="plain">SECTIONNAME</arg> depending on the
+                syntax used).
+            </para></listitem>
+            <listitem><para>
+                <command>ostree config set</command> sets the value of
+                <arg choice="plain">KEYNAME</arg> in the group <arg choice="plain">GROUPNAME</arg>
+                to <arg choice="plain">VALUE</arg>.
+            </para></listitem>
+            <listitem><para>
+                <command>ostree config unset</command> removes the key
+                <arg choice="plain">KEYNAME</arg> from the group <arg choice="plain">GROUPNAME</arg>
+                so that OSTree uses the default value for it. It is not an
+                error for the specified <arg choice="plain">GROUPNAME</arg> or
+                <arg choice="plain">KEYNAME</arg> not to exist.
+            </para></listitem>
+        </itemizedlist>
     </refsect1>
 
     <refsect1>
@@ -77,5 +100,6 @@ Boston, MA 02111-1307, USA.
         <para><command>$ ostree config get core.mode</command></para>
         <para>bare</para>
         <para><command>$ ostree config set --group='remote "myremote"' url http://example.com/repo</command></para>
+        <para><command>$ ostree config unset core.lock-timeout-secs</command></para>
     </refsect1>
 </refentry>
index 4368f50c663c27c7b1b0dde763034deeaf9b3080..913ed66d0141316e68a88f8c4c9903c5c0af624f 100644 (file)
@@ -73,7 +73,7 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
   g_autofree char *key = NULL;
   GKeyFile *config = NULL;
 
-  context = g_option_context_new ("(get KEY|set KEY VALUE)");
+  context = g_option_context_new ("(get KEY|set KEY VALUE|unset KEY)");
 
   if (!ostree_option_context_parse (context, options, &argc, &argv, invocation, &repo, cancellable, error))
     goto out;
@@ -155,6 +155,47 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
 
       g_print ("%s\n", value);
     }
+  else if (!strcmp (op, "unset"))
+    {
+      g_autoptr(GError) local_error = NULL;
+      if (opt_group)
+        {
+          if (argc < 3)
+            {
+              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "Group name and key must be specified");
+              goto out;
+            }
+          section = g_strdup(opt_group);
+          key = g_strdup(argv[2]);
+        }
+      else
+        {
+          if (argc < 3)
+            {
+              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "KEY must be specified");
+              goto out;
+            }
+          section_key = argv[2];
+          if (!split_key_string (section_key, &section, &key, error))
+            goto out;
+        }
+
+      config = ostree_repo_copy_config (repo);
+      if (!g_key_file_remove_key (config, section, key, &local_error))
+        {
+          if (!g_error_matches (local_error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) &&
+              !g_error_matches (local_error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND))
+            {
+              g_propagate_error (error, g_steal_pointer (&local_error));
+              goto out;
+            }
+        }
+
+      if (local_error == NULL && !ostree_repo_write_config (repo, config, error))
+        goto out;
+    }
   else
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
index b1ea3e5e355847126e6fcf818050343e963b4591..62f63006459daa776b31e519f5f53bc1c27ecbfe 100755 (executable)
@@ -23,7 +23,7 @@ set -euo pipefail
 
 . $(dirname $0)/libtest.sh
 
-echo '1..2'
+echo '1..3'
 
 ostree_repo_init repo
 ${CMD_PREFIX} ostree remote add --repo=repo --set=xa.title=Flathub --set=xa.title-is-set=true flathub https://dl.flathub.org/repo/
@@ -53,3 +53,27 @@ assert_file_has_content repo/config "Nightly Flathub"
 assert_file_has_content repo/config "false"
 assert_file_has_content repo/config "http://example.com/ostree/"
 echo "ok config set"
+
+# Check that "ostree config unset" works
+${CMD_PREFIX} ostree config --repo=repo set core.lock-timeout-secs 60
+assert_file_has_content repo/config "lock-timeout-secs=60"
+${CMD_PREFIX} ostree config --repo=repo unset core.lock-timeout-secs
+assert_not_file_has_content repo/config "lock-timeout-secs="
+
+# Check that "ostree config get" errors out on the key
+if ${CMD_PREFIX} ostree config --repo=repo get core.lock-timeout-secs 2>err.txt; then
+    assert_not_reached "ostree config get should not work after unsetting a key"
+fi
+assert_file_has_content err.txt "error: Key file does not have key “lock-timeout-secs” in group “core”"
+
+# Check that it's idempotent
+${CMD_PREFIX} ostree config --repo=repo unset core.lock-timeout-secs
+assert_not_file_has_content repo/config "lock-timeout-secs="
+
+# Check that the group doesn't need to exist
+${CMD_PREFIX} ostree config --repo=repo unset --group='remote "aoeuhtns"' 'xa.title'
+
+# Check that the key doesn't need to exist
+${CMD_PREFIX} ostree config --repo=repo set --group='remote "aoeuhtns"' 'xa.title-is-set' 'false'
+${CMD_PREFIX} ostree config --repo=repo unset --group='remote "aoeuhtns"' 'xa.title'
+echo "ok config unset"